package com.sesame.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sesame.user.bean.User;
import com.sesame.user.dao.UserMapper;
import com.sesame.user.service.UserService;
import kim.sesame.common.result.ApiResult;
import kim.sesame.common.web.controller.AbstractWebController;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * UserController
 *
 * @author admin
 * @date 2019-01-30 13:03:18
 * @Description:
 */
@CommonsLog
@RestController
@RequestMapping("/user")
public class UserController extends AbstractWebController {

    @Resource
    private UserService userService;

    /**
     * 查询list
     * http://127.0.0.1:8055/user/list
     * @author admin
     * @date 2019-01-30 13:03:18
     * @Description:
     */
    @RequestMapping("/list")
    public ApiResult list(User bean) {

        List<User> list = userService.searchList(bean);

        return success(list);
    }

    /**
     * 分页查询
     *
     * @author admin
     * @date 2019-01-30 13:03:18
     * @ pageNum : 页码,默认1
     * @ pageSize :  页面大小
     */
//    @RequestMapping("/listPage")
//    public ApiResult listPage(User bean, GPage gPage) {
//
//        Page<User> pages = PageHelper.startPage(gPage.getPageNum(), gPage.getPageSize())
//                .doSelectPage(() -> userService.searchList(bean));
//        List<User> list = pages.getResult();
//
//        return success(list, PageUtil.recount(gPage, pages));
//    }

    /**
     * 保存
     *
     * @author admin
     * @date 2019-01-30 13:03:18
     * @Description:
     */
//    @RequestMapping(value = "/save", method = {RequestMethod.POST})
//    public ApiResult save(User bean) {
//        int res = 0;
//        if (StringUtils.isNotEmpty(bean.getId())) {
//            // 主键不为空, 修改数据
//            bean.initUpdate("0000");
//            res = userService.update(bean);
//        } else {
//            //主键为空, 新增数据
//            bean.initCreateAndId("0000");
//            res = userService.add(bean);
//        }
//        return success(res);
//    }

    /**
     * 删除
     *
     * @author admin
     * @date 2019-01-30 13:03:18
     * @Description:
     */
    @RequestMapping("/delete")
    public ApiResult delete(String ids, HttpServletRequest request) {
        int res = userService.delete(ids);
        return success(res);
    }

    /**
     * 查询详情
     *
     * @author admin
     * @date 2019-01-30 13:03:18
     * @Description:
     */
    @RequestMapping("/searchDetail")
    public ApiResult searchDetail(User bean, HttpServletRequest request) {
        bean = userService.search(bean);
        return success(bean);
    }

    /**
     * http://127.0.0.1:8055/user/queryMap?id=1
     * @param bean
     * @return
     */
    @RequestMapping("/queryMap")
    public ApiResult queryMap(User bean) {
        return success(userService.searchMap(bean));
    }

    /**
     * http://127.0.0.1:8055/user/queryGMap?id=1
     * @param bean
     * @return
     */
    @RequestMapping("/queryGMap")
    public ApiResult queryGMap(User bean) {
        return success(userService.searchGMap(bean));
    }

    @SuppressWarnings("all")
    @Autowired
    UserMapper userMapper;

    /**
     * http://127.0.0.1:8055/user/mplist
     * @return
     */
    @RequestMapping("/mplist")
    public ApiResult mplist() {

        List<User> list = userMapper.selectList(new QueryWrapper<User>()
                .lambda()
                .like(User::getName, "o")
        );

        return success(list);
    }

    /**
     * http://127.0.0.1:8055/user/queryByid?id=1
     * @param id
     * @return
     */
    @RequestMapping("/queryByid")
    public ApiResult queryByid(String id) {
        User user = userMapper.selectById(id);

        return success(user);
    }
}
